﻿import 'package:flutter/material.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'dart:ui' as ui;
import 'event_summary_logic.dart';
import '../../../../gen/assets.gen.dart';
import '../../../../common/styles/theme_manager.dart';
import '../../../../widgets/fixed_header_with_tabs.dart';
import '../../calendar/simple_calendar_view.dart';

/// 事件小计页面
class EventSummaryView extends GetView<EventSummaryLogic> {
  const EventSummaryView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: SafeArea(
        child: Column(
          children: [
            // 固定的头部（标题 + 日周月切换）
            Obx(() => FixedHeaderWithTabs(
                  title: '事件小计',
                  selectedIndex: controller.state.selectedPeriod.value,
                  onTabChanged: (index) => controller.changePeriod(index),
                  actions: [
                    IconButton(
                      icon: Assets.status.calendar.image(
                        width: 24.w,
                        height: 24.w,
                        color: Colors.white,
                      ),
                      onPressed: () async {
                        final selectedDate = await Get.to(() =>
                            const SimpleCalendarView(
                                calendarType: CalendarType.eventSubtotal));
                        if (selectedDate != null && selectedDate is String) {
                          controller.loadDataByDate(selectedDate);
                        }
                      },
                    ),
                  ],
                )),
            // 可滚动的内容区域
            Expanded(
              child: Stack(
                children: [
                  // 渐变背景
                  Positioned(
                    top: 0,
                    left: 0,
                    right: 0,
                    height: 200.h,
                    child: Container(
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          begin: Alignment.topRight,
                          end: Alignment.bottomRight,
                          colors: ThemeManager.to.backgroundGradientColors,
                          stops: const [0.1, 0.3, 0.7, 1.0],
                        ),
                      ),
                    ),
                  ),
                  // 主要内容
                  SingleChildScrollView(
                    child: Padding(
                      padding: EdgeInsets.symmetric(horizontal: 16.w),
                      child: Column(
                        children: [
                          SizedBox(height: 20.h),

                          // 事件列表
                          _buildEventList(),

                          SizedBox(height: 32.h),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }



  /// 构建事件列表
  Widget _buildEventList() {
    return Obx(() {
      final groupedEvents = controller.state.groupedEvents;

      if (groupedEvents.isEmpty) {
        return _buildEmptyState();
      }

      return Column(
        children: groupedEvents.entries.map((entry) {
          final date = entry.key;
          final events = entry.value;
          return _buildDateGroup(date, events);
        }).toList(),
      );
    });
  }

  /// 构建空状态
  Widget _buildEmptyState() {
    return Container(
      padding: EdgeInsets.all(40.w),
      child: Center(
        child: Column(
          children: [
            Icon(
              Icons.event_note,
              color: Colors.grey[600],
              size: 48.sp,
            ),
            SizedBox(height: 16.h),
            Text(
              '暂无事件记录',
              style: TextStyle(
                fontSize: 16.sp,
                color: Colors.grey[400],
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 构建日期分组
  Widget _buildDateGroup(String date, List<EventItem> events) {
    return Container(
      margin: EdgeInsets.only(bottom: 16.h),
      padding: EdgeInsets.all(16.w),
      decoration: BoxDecoration(
        color: const Color.fromRGBO(3, 0, 3, 1),
        borderRadius: BorderRadius.circular(20.r),
        border: Border.all(
          color: const Color.fromRGBO(33, 33, 33, 1),
          width: 1.0,
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 日期标题
          Text(
            date,
            style: TextStyle(
              fontSize: 14.sp,
              color: Colors.grey[400],
              fontWeight: FontWeight.w500,
            ),
          ),
          SizedBox(height: 12.h),

          // 事件列表
          ...events.map((event) => _buildEventItem(event)).toList(),
        ],
      ),
    );
  }

  /// 构建单个事件项
  Widget _buildEventItem(EventItem event) {
    return Container(
      margin: EdgeInsets.only(bottom: 12.h),
      padding: EdgeInsets.all(12.w),
      decoration: BoxDecoration(
        color: const Color(0xFF282528),
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: Row(
        children: [
          // 分数圆圈
          Container(
            width: 50.w,
            height: 50.w,
            decoration: BoxDecoration(
              color: event.isPositive
                  ? const Color(0xFF3CE89B).withValues(alpha: 0.2)
                  : const Color(0xFFED5C2F).withValues(alpha: 0.2),
              shape: BoxShape.circle,
            ),
            child: Center(
              child: Text(
                '${event.score}分',
                style: TextStyle(
                  fontSize: 14.sp,
                  color: event.isPositive
                      ? const Color(0xFF3CE89B)
                      : const Color(0xFFED5C2F),
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
          SizedBox(width: 12.w),

          // 事件内容
          Expanded(
            child: Text(
              event.content,
              style: TextStyle(
                fontSize: 14.sp,
                color: Colors.white,
              ),
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
            ),
          ),

          SizedBox(width: 12.w),

          // 时间
          Text(
            event.time,
            style: TextStyle(
              fontSize: 12.sp,
              color: Colors.grey[400],
            ),
          ),
        ],
      ),
    );
  }
}

/// 事件项模型
class EventItem {
  final String content;
  final int score;
  final bool isPositive;
  final String time;

  EventItem({
    required this.content,
    required this.score,
    required this.isPositive,
    required this.time,
  });
}
